package com.bluegalaxy.nhb.snservice.core.util;


import com.bluegalaxy.nhb.snservice.common.utils.AppConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 *
 * @author:
 * @date: 2019-08-27
 * @time: 13:37
 * @description: To change this template use File | Settings | File Templates.
 */
public class MailUtils {

    final static String mailhost = "mail.qq.com";

    private static final String MAIL_HOST = AppConfig.getStringByProp("props/mail", "system.mail.host");
    private static final String MAIL_PORT = AppConfig.getStringByProp("props/mail", "system.mail.port");
    private static final String MAIL_USERNAME = AppConfig.getStringByProp("props/mail", "system.mail.username");
    private static final String MAIL_PASSWORD = AppConfig.getStringByProp("props/mail", "system.mail.password");
    private static final String MAIL_SMS = AppConfig.getStringByProp("props/mail", "system.mail.sms");
    private static Logger logger = LoggerFactory.getLogger(MailUtils.class);
    private static JavaMailSenderImpl mailSender = null;

    static {
        synchronized (MailUtils.class) {
            if (mailSender == null) {
                mailSender = new JavaMailSenderImpl();
                //指定用来发送Email的邮件服务器主机名
                mailSender.setHost(MAIL_HOST);
                //默认端口，标准的SMTP端口
                mailSender.setPort(Integer.valueOf(MAIL_PORT));
                //用户名
                mailSender.setUsername(MAIL_USERNAME);
                //密码
                mailSender.setPassword(MAIL_PASSWORD);
            }
        }
    }

    /**
     * 发送文本短信
     *
     * @param to
     * @param cc
     * @param subject
     * @param body
     */
    public static void sendSimpleText(String[] to, String[] cc, String subject, String body) {
        //消息构造器
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(MAIL_USERNAME);
        //收件人
        message.setTo(to);
        message.setCc(cc);
        //主题
        message.setSubject(subject);
        //正文
        message.setText(body);
        mailSender.send(message);
    }

    /**
     * 给手机发短信，多的逗号分隔,
     *
     * @param phones
     * @param body
     */
    public static void sentSmsText(String phones, String body) {

        //消息构造器
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(MAIL_USERNAME);
        message.setTo(MAIL_SMS);
        //主题
        message.setSubject(phones);
        //正文
        message.setText(body);
        mailSender.send(message);
    }


    /**
     * 发送邮件工具类，有默认的用户名密码
     *
     * @param to
     * @param cc
     * @param bcc
     * @param subject
     * @param message
     * @param csvFileList
     * @param isHtml      是否以html形式翻译正文
     * @throws MessagingException
     * @throws IOException
     */
    public static void postMailUtilDefault(String to, String cc, String bcc, String subject, String message, ArrayList<File> csvFileList, boolean isHtml) throws MessagingException, IOException {
        MailUtil.postMailUtil(to, cc, bcc, subject, message, MAIL_USERNAME, csvFileList, MAIL_USERNAME, MAIL_PASSWORD, isHtml);
    }

    /**
     * 发邮件工具类，正文不翻译成html
     *
     * @param to
     * @param cc
     * @param bcc
     * @param subject
     * @param message
     * @param csvFileList
     * @throws MessagingException
     * @throws IOException
     */
    public static void postMailUtilNoHtml(String to, String cc, String bcc, String subject, String message, ArrayList<File> csvFileList) throws MessagingException, IOException {
        postMailUtilDefault(to, cc, bcc, subject, message, csvFileList, false);
    }
}
